package com.daisuxia.web.util;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.web.pojo.BorrowOrder;
import com.daisuxia.web.pojo.Repayment;
import com.daisuxia.web.pojo.RepaymentDetail;
import com.daisuxia.web.pojo.User;
import com.daisuxia.web.service.IBorrowOrderService;
import com.daisuxia.web.service.IRepaymentDetailService;
import com.daisuxia.web.service.IRepaymentService;
import com.daisuxia.web.service.IUserService;
import com.daisuxia.web.test.ThreadPool;

import redis.clients.jedis.JedisCluster;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by IntelliJ IDEA
 * User : zhangsh
 * Date : 2017/2/17 0017
 * Time : 13:00
 */
public class WithholdThread extends Thread{
    private Integer repaymentId;
    private IRepaymentService repaymentService;
    private IRepaymentDetailService repaymentDetailService;
    private IBorrowOrderService borrowOrderService;
    private SimpleDateFormat dateFormat;
    private String repaymentTime;
    private Logger logger;
    private IUserService userService;
    private JedisCluster jedisCluster;


    public WithholdThread(Logger logger, Integer repaymentId, String repaymentTime, SimpleDateFormat dateFormat, IUserService userService, IRepaymentService repaymentService, IRepaymentDetailService repaymentDetailService, IBorrowOrderService borrowOrderService,JedisCluster jedisCluster)
    {
        this.logger = logger;
        this.repaymentId = repaymentId;
        this.repaymentTime = repaymentTime;
        this.dateFormat = dateFormat;
        this.userService = userService;
        this.repaymentService = repaymentService;
        this.repaymentDetailService = repaymentDetailService;
        this.borrowOrderService = borrowOrderService;
        this.jedisCluster = jedisCluster;
    }
    public void run()
    {
        try {
            logger.error("自动代扣开始 withhold repaymentId = " + repaymentId);
            String repayStatus = jedisCluster.get("REPAYMENT_REPAY_WITHHOLD_" + repaymentId);
            if("true".equals(repayStatus)){
                return;
            }
            //锁定该笔还款，防止其他渠道再次扣款
            jedisCluster.setex("REPAYMENT_REPAY_WITHHOLD_"+repaymentId,5 * 60, "true");
            
            Repayment re = repaymentService.selectByPrimaryKey(repaymentId);
            if(dateFormat.format(re.getRepaymentTime()).equals(repaymentTime) && re.getStatus().equals(BorrowOrder.STATUS_HKZ)) {
                User user = userService.searchByUserid(re.getUserId());
                ServiceResult result = repaymentService.withhold(re, user, Repayment.TASK_WITHHOLD);
                /* if (result.getCode().equals(ServiceResult.SUCCESS)) {
                RepaymentDetail detail = new RepaymentDetail();
                detail.setUserId(re.getUserId());
                detail.setAssetRepaymentId(re.getId());
                detail.setTrueRepaymentMoney(re.getRepaymentAmount() - re.getRepaymentedAmount());
                detail.setCreatedAt(new Date());
                detail.setOrderId(result.getExt().toString());
                detail.setRepaymentType(RepaymentDetail.TYPE_BANK_CARD_AUTO);
                detail.setRemark("定时任务代扣");
                detail.setUserId(re.getUserId());
                detail.setUpdatedAt(new Date());
                detail.setStatus(RepaymentDetail.STATUS_SUC);
                detail.setAssetOrderId(re.getAssetOrderId());
                repaymentDetailService.insertSelective(detail);
                repaymentService.repay(re, detail);

                // 如果是未逾期的还款，调用提额
                if (re.getLateDay() == 0) {
                    user = userService.searchByUserid(re.getUserId());
                    logger.error("repay to Mention Money ing ----------------->");
                    borrowOrderService.addUserLimit(user);
                }
            }*/
                //交易成功或者失败，都要删除redis中锁定的该笔还款，防止如果交易失败，其他渠道不能还款的请款
                if(!"100".equals(result.getCode())){
                	jedisCluster.del("REPAYMENT_REPAY_WITHHOLD_"+re.getId());
                }
            }
        } catch (Exception e) {
            logger.error("repay withhold error repaymentId = " + repaymentId, e);
        }
    }
}
